<?php
namespace App\HttpController;

use EasySwoole\EasySwoole\ServerManager;
use EasySwoole\HttpAnnotation\AnnotationController;
use App\Models\History;
use App\Models\Project;
class BaseController extends AnnotationController
{
    public function index()
    {
        $this->actionNotFound('index');
    }

    /**
     * 获取用户的真实IP
     * @param string $headerName 代理服务器传递的标头名称
     * @return string
     */
    protected function clientRealIP($headerName = 'x-real-ip')
    {
        $server = ServerManager::getInstance()->getSwooleServer();
        $client = $server->getClientInfo($this->request()->getSwooleRequest()->fd);
        $clientAddress = $client['remote_ip'];
        $xri = $this->request()->getHeader($headerName);
        $xff = $this->request()->getHeader('x-forwarded-for');
        if ($clientAddress === '127.0.0.1') {
            if (!empty($xri)) {  // 如果有 xri 则判定为前端有 NGINX 等代理
                $clientAddress = $xri[0];
            } elseif (!empty($xff)) {  // 如果不存在 xri 则继续判断 xff
                $list = explode(',', $xff[0]);
                if (isset($list[0])) $clientAddress = $list[0];
            }
        }
        return $clientAddress;
    }

    protected function input($name, $default = null)
    {
        $value = $this->request()->getRequestParam($name);
        return $value ?? $default;
    }


    protected  function logapprove($userid,$approve,$itemid,$idea,$isgroup,$usertype){
        $item = Project::create()->where(['id'=>intval($itemid)])->get();
        $res = History::create(['userid'=>$userid,'approveres'=>$approve,'idea'=>$idea,'isgroup'=>$isgroup,
        'itemid'=>$itemid,'addtime'=>time(),'usertype'=>$usertype,'uuid'=>$item['approveuuid']])->save();
        return $res;


    }


    public function showPageStringAsAJAX( $jumpFun, $currentPage, $pageSize, $totalCount){
        $pageSize = $pageSize == 0 ? 3 : $pageSize;
        $totalPages = floor(($totalCount + $pageSize - 1) / $pageSize); //总页数
       $output = "";
      if ($totalPages > 1)
       {
           {//处理首页连接
               $output .= "<a class='pages-a' href='javascript:void(0);'  onclick='".$jumpFun."(1,".$pageSize.")'>首页</a>";
       
           }
           if ($currentPage > 1)
          {//处理上一页的连接
               $output .= "<a class='pages-a' href='javascript:void(0);' onclick='".$jumpFun."(".($currentPage - 1).", ".$pageSize.")'>上一页</a>";
           }
          else
           {
               // output.Append("<span class='pageLink'>上一页</span>");
           }
       
           $output .= " ";
           $currint = 5;
           for ($i = 0; $i <= 10; $i++)
        {//一共最多显示10个页码，前面5个，后面5个
               if (($currentPage + $i - $currint) >= 1 && ($currentPage + $i - $currint) <= $totalPages)
               {
                   if ($currint == $i)
                   {//当前页处理

                       $output .= "<a class='pages-a on'  href='javascript:void(0);'>$currentPage</a>";
                   }
                  else
                   {//一般页处理
                       $output .= "<a  class='pages-a' href='javascript:void(0);' onclick='".$jumpFun."(".($currentPage + $i - $currint).",".$pageSize.")'>".($currentPage + $i - $currint)."</a>";
                   }
               }
               $output .= " ";
           }
           if ($currentPage < $totalPages)
           {//处理下一页的链接
               $output .= "<a  class='pages-a' href='javascript:void(0);' onclick='".$jumpFun."(".($currentPage + 1).",".$pageSize.")'>下一页</a>";
           }
           else
        {
            //output.Append("<span class='pageLink'>下一页</span>");
           }
           $output .=" ";
           if ($currentPage != $totalPages)
           {
               //处理末页的链接
               $output .= "><a  href='javascript:void(0);' class='pages-a' onclick='".$jumpFun."(".$totalPages.", ".$pageSize.")'>末页</a>";
           }
           
     }
       //$output .= "第".$currentPage."页 / 共".$totalPages."页";//这个统计加不加都行
       
       return $output;
   }






   public function showPageStringAsAJAXForitem( $jumpFun, $currentPage, $pageSize, $totalCount,$owner){
    $pageSize = $pageSize == 0 ? 3 : $pageSize;
    $totalPages = floor(($totalCount + $pageSize - 1) / $pageSize); //总页数
   $output = "";
  if ($totalPages > 1)
   {
       {//处理首页连接
           $output .= "<a class='pages-a' href='javascript:void(0);'  onclick='".$jumpFun."(1,".$pageSize.")'>首页</a>";
   
       }
       if ($currentPage > 1)
      {//处理上一页的连接
           $output .= "<a class='pages-a' href='javascript:void(0);' onclick='".$jumpFun."(".($currentPage - 1).", ".$pageSize.")'>上一页</a>";
       }
      else
       {
           // output.Append("<span class='pageLink'>上一页</span>");
       }
   
       $output .= " ";
       $currint = 5;
       for ($i = 0; $i <= 10; $i++)
    {//一共最多显示10个页码，前面5个，后面5个
           if (($currentPage + $i - $currint) >= 1 && ($currentPage + $i - $currint) <= $totalPages)
           {
               if ($currint == $i)
               {//当前页处理

                   $output .= "<a class='pages-a on'  href='javascript:void(0);'>$currentPage</a>";
               }
              else
               {//一般页处理
                  $output .= "<a  class='pages-a' href='javascript:void(0);' onclick='".$jumpFun."(".($currentPage + $i - $currint).",".$pageSize.")'>".($currentPage + $i - $currint)."</a>";
            
               
               
               
                }
           }
           $output .= " ";
       }
       if ($currentPage < $totalPages)
       {//处理下一页的链接
           $output .= "<a  class='pages-a' href='javascript:void(0);' onclick='".$jumpFun."(".($currentPage + 1).",".$pageSize.")'>下一页</a>";
       }
       else
    {
        //output.Append("<span class='pageLink'>下一页</span>");
       }
       $output .=" ";
       if ($currentPage != $totalPages)
       {
           //处理末页的链接
           $output .= "<a  href='javascript:void(0);' class='pages-a' onclick='".$jumpFun."(".$totalPages.", ".$pageSize.")'>末页</a>";
       }
       
 }
   //$output .= "第".$currentPage."页 / 共".$totalPages."页";//这个统计加不加都行
   
   return $output;
}



}
